package com.sum.common.panling;

import android.util.Log;

import com.panling.deviceapi.DeviceAPI;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Optional;

public class PowerUtils {
    private static final String TAG = "PowerUtils";
    private static final String VOL33 = "/sys/devices/platform/module_power/rfid_en_3v3";
    private static final String VOL5 = "/sys/devices/platform/module_power/rfid_print_5v";
    private static final String POWER = "/sys/devices/platform/module_power/vdd_io_host_en";
    private static final String UART_SWITCH = "/sys/devices/platform/module_power/uart_switch_1v8";

    // 使用LazyHolder方式实现单例
    private static class LazyHolder {
        static final PowerUtils INSTANCE = new PowerUtils();
    }

    // 私有构造函数
    private PowerUtils() {
    }

    // 获取单例实例
    public static PowerUtils getInstance() {
        return LazyHolder.INSTANCE;
    }

    /**
     * 写入UHF文件
     * @param path 文件路径
     * @param value 写入的字节数组
     */
    private static void writeUhfFile(String path, byte[] value) {
        if (path == null || value == null) {
            return;
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path);
            fos.write(value);
            fos.flush();
        } catch (IOException e) {
            Log.d(TAG, "writeUhfFile error: " + e.getMessage());
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    Log.d(TAG, "Close file error: " + e.getMessage());
                }
            }
        }
    }

    /**
     * 上电操作
     */
    private void panlingPowerUp() {
        Log.d(TAG, "panling_power_up:");
        DeviceAPI.getInstance().RfidPoweron();
        /*
        try {
            writeUhfFile(UART_SWITCH, "0".getBytes());
            Thread.sleep(20);
            writeUhfFile(POWER, "1".getBytes());
            Thread.sleep(20);
            writeUhfFile(VOL33, "1".getBytes());
            Thread.sleep(20);
            writeUhfFile(VOL5, "1".getBytes());
            Thread.sleep(10);
        } catch (Exception e) {
            Log.e(TAG, "Power up interrupted", e);
        }

         */
    }

    public void setPowerOn(boolean isOn) {
        if (isOn) {
            panlingPowerUp();
        } else {
            panlingPowerDown();
        }
    }

    /**
     * 断电操作
     */
    private void panlingPowerDown() {
        Log.d(TAG, "panling_power_down:");
        DeviceAPI.getInstance().RfidPowerdown();
        /*
        try {
            writeUhfFile(VOL5, "0".getBytes());
            Thread.sleep(10);
            writeUhfFile(VOL33, "0".getBytes());
            Thread.sleep(10);
            writeUhfFile(POWER, "0".getBytes());
            Thread.sleep(10);
            writeUhfFile(UART_SWITCH, "1".getBytes());
        } catch (Exception e) {
            Log.e(TAG, "Power down interrupted", e);
        }

         */
    }
}